package cn.edu.ctbu.demo1.webapi;

import cn.edu.ctbu.demo1.core.RUtil;
import cn.edu.ctbu.demo1.domain.Admin;
import cn.edu.ctbu.demo1.domain.Student;
import cn.edu.ctbu.demo1.domain.Teacher;
import cn.edu.ctbu.demo1.exception.REnum;
import cn.edu.ctbu.demo1.service.AdminService;
import cn.edu.ctbu.demo1.service.StudentService;
import cn.edu.ctbu.demo1.service.TeacherService;
import cn.edu.ctbu.demo1.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/webapi")
public class LoginRestController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private AdminService adminService;
//    @RequestMapping("/student")
//    public R student(String name ,String password) throws  Exception{
//         Student student =  studentService.findByNameAndPassword(name,password);
//         return RUtil.success(student);
//    }

    @GetMapping("/user")
    public R user(HttpServletRequest httpServletRequest, String username, String password) {
        /**
         * 判断用户名和密码是否存在（学生）
         */
        List<Student> students = null;
        List<Teacher> teachers = null;
        List<Admin>   admins = null;

//        String manager = "世界你好";
//        String word = "666666";
        HttpSession session = httpServletRequest.getSession();
        if(username.length() == 10){
            /**
             * 学生登陆
             */
            students = studentService.findByNoAndPassword(username, password);
            if (students.size() > 0) {
                //如果存在,写入session
                session.setAttribute("studentId", students.get(0).getId());
                session.setAttribute("student", students.get(0));
                session.setAttribute("studentNo",students.get(0).getNo());
                return RUtil.success(null);//成功
            }
            else {
                return RUtil.error(REnum.LOGIN_ERR);
            }
        }
        else if(username.length() == 5){
            /**
             * 教师登陆
             */;
            teachers =  teacherService.findByNoAndPassword(username, password);
            if (teachers.size() > 0) {
                //如果存在,写入session

                session.setAttribute("teacherId", teachers.get(0).getId());
                session.setAttribute("teacher", teachers.get(0));
                session.setAttribute("teacherName",teachers.get(0).getName());
                return RUtil.successTeacher(null);//成功
            }
            else return RUtil.error(REnum.LOGIN_ERR);

        }else if(username.length() == 4){
            /**
             * 管理员登陆
             */
            admins = adminService.findByNoAndPassword(username, password);
            if (admins.size() > 0) {
                //如果存在,写入session
                session.setAttribute("managerId", admins.get(0).getId());
                return RUtil.successManage(null);//成功
            }
            else {
                return RUtil.error(REnum.LOGIN_ERR);
            }
        }else {
            return RUtil.error(REnum.LOGIN_ERR);
        }




}

    /**
     * 检测是否登陆，没有登陆转到登陆界面
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/login/check")
    public R isLogin(HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        if(session.getAttribute("studentId") ==null && session.getAttribute("teacherId") == null
             &&session.getAttribute("managerId") == null){
            return RUtil.success(REnum.LOGIN_ERR);
        }else {
            return RUtil.success(REnum.SUCCESS);
        }
    }
    @GetMapping("/quit/login")
    public R quitLogin(HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        session.removeAttribute("studentId");
        session.removeAttribute("managerId");
        session.removeAttribute("teacherId");
        return RUtil.success(REnum.SUCCESS);

    }
}
